import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:overload_guard/app_theme.dart';
import 'package:overload_guard/main.dart';
import 'package:overload_guard/pages/ocr_page.dart';
import 'package:overload_guard/services/api_service.dart';
import 'package:overload_guard/services/db_service.dart';
import 'package:overload_guard/widgets/buttons.dart';
import 'package:overload_guard/widgets/dialogs.dart';

class DataInputPage extends StatefulWidget {
  @override
  State<DataInputPage> createState() => _DataInputPageState();
}

class _DataInputPageState extends State<DataInputPage> {
  TextEditingController carNumController = TextEditingController();
  TextEditingController carBoxController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("信息录入", style: TextStyle(color: Colors.white)),
        backgroundColor: AppTheme.primaryColor,
        centerTitle: true,
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(left: 30, right: 30),
        child: ListView(
          // mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(padding: EdgeInsets.only(top: 40)),
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Expanded(
                  child: Text(
                    "车板号",
                    style: Theme.of(context).textTheme.bodyLarge,
                  ),
                  flex: 4,
                ),
                Expanded(
                  child: TextField(
                    controller: carNumController,
                    decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.grey)),
                      focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: AppTheme.primaryColor)),
                      hintText: "输入车板号",
                    ),
                  ),
                  flex: 10,
                ),
                IconButton(
                    onPressed: () {
                      Get.to(OcrPage())!.then((value) {
                        if (value != null) {
                          carNumController.text = value;
                        }
                      });
                    },
                    icon: Icon(Icons.camera_alt)),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 10)),
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Expanded(
                  flex: 4,
                  child: Text(
                    "集装箱编号",
                    style: Theme.of(context).textTheme.bodyLarge,
                  ),
                ),
                Expanded(
                  child: TextField(
                      controller: carBoxController,
                      decoration: InputDecoration(
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey)),
                        focusedBorder: OutlineInputBorder(
                            borderSide:
                                BorderSide(color: AppTheme.primaryColor)),
                        hintText: "输入集装箱编号",
                      )),
                  flex: 10,
                ),
                IconButton(
                    onPressed: () {
                      Get.to(OcrPage())!.then((value) {
                        if (value != null) {
                          carBoxController.text = value;
                        }
                      });
                    },
                    icon: Icon(Icons.camera_alt)),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 30)),
            mainSubmitButton("确认", () async {
              var result = await APIService.shippingInputRecord(
                  containerNum: carBoxController.text,
                  templateNum: carNumController.text,
                  userId: await DBService.getData(DBKey.userId));
              print('确认 result:$result');

              if (result != null) {
                Dialogs.showSuccess();

                Get.offAll(MyHomePage());
              }
            }, context, AppTheme.primaryColor),
            Padding(padding: EdgeInsets.only(top: 10)),
            mainSubmitButton("退出", () {
              Get.back();
            }, context, Colors.grey),
            Padding(padding: EdgeInsets.only(top: 10)),
          ],
        ),
      ),
    );
  }
}
